﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spring.Context;
using Spring.Context.Support;

namespace Infrastructures
{
    public static class FactoryCreator
    {
        private static IApplicationContext springContext;

        private static IApplicationContext SpringContext
        {
            get
            {
                if (springContext == null)
                    Initialize();

                return springContext;
            }
            set
            {
                springContext = value;
            }
        }

        public static void Initialize()
        {
            if (springContext == null)
            {
                SpringContext = ContextRegistry.GetContext();
            }
        }

        public static void Refresh()
        {
            SpringContext = ContextRegistry.GetContext();
        }

        public static T Create<T>()
        {
            Type type = typeof(T);
            string typeName = type.Name;
            object obj = SpringContext.GetObject(typeName);

            return (T)obj;
        }

        public static object Create(string name)
        {
            object obj = SpringContext.GetObject(name);

            return obj;
        }

        public static object Create(string name, Type type)
        {
            object obj = SpringContext.GetObject(name, type);

            return obj;
        }
    }
}
